﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cafe.Models;

namespace Cafe.Controllers
{
    public class HomeController : Controller
    {
        List<CafePlace> cafes = Singleton.Instance.Cafes;
        List<Review> reviews = Singleton.Instance.Reviews;

        public ActionResult Index(int id = 0)
        {
            IndexVM bucket = new IndexVM();
            bucket.NextIndex = id + 3;
            bucket.PrevIndex = id - 3;
            if (bucket.NextIndex >= cafes.Count())
            {
                bucket.NextIndex = id;
            }
            if (bucket.PrevIndex <= 0)
            {
                bucket.PrevIndex = 0;
            }
            bucket.cafes = cafes.Skip(id).Take(3).ToList(); 
            return View(bucket);
        }
        [HttpGet]
        public ActionResult Add()
        {
           

            return View();
        }

        [HttpPost]
        public ActionResult Add(string name,string location, string category)
        {
            CafePlace cafe = new CafePlace(category: category, name: name, location: location);
            cafes.Add(cafe);
            return RedirectToAction("Detail/" + cafe.ID);
        }

        public ActionResult Detail(int id) 
        {
            CafePlace cafe = cafes.Where(x => x.ID == id).FirstOrDefault();
            DetailVM bucket = new DetailVM();
            bucket.Cafe = cafe;
            bucket.Reviews = reviews.Where(x => x.CafeID == cafe.ID).ToList();
            bucket.Cafe.SetAvg(bucket.Reviews);
            return View(bucket);
        }
        [HttpGet]
        public ActionResult AddReview(int cafeID)
        {
            CafePlace cafe = cafes.Where(x => x.ID == cafeID).FirstOrDefault();
            return View(cafe);
        }
        [HttpPost]
        public ActionResult AddReview(int ID, string user, string comment, Rating rating)
        {
            Review review = new Review(ID, user, rating, comment);
            reviews.Add(review);
            return RedirectToAction("Detail/" + ID);
        }
        [HttpPost]
        public ActionResult DeleteReview(int id)
        {
            Review review = reviews.Where(x => x.ID == id).FirstOrDefault();
            int cafeid = review.CafeID;
            reviews.Remove(review);
            return RedirectToAction("Detail/" + cafeid);
        }
        [HttpPost]
        public ActionResult DeleteCafe(int id)
        {
            CafePlace cafe = cafes.Where(x => x.ID == id).FirstOrDefault();
            cafes.Remove(cafe);
            reviews.RemoveAll(x => x.CafeID == id);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult EditCafe(int id)
        {
            CafePlace cafe = cafes.Where(x => x.ID == id).FirstOrDefault();
            return View(cafe);
        }
        [HttpPost]
        public ActionResult EditCafe(int id, string name, string location, string category)
        {
            CafePlace cafe = cafes.Where(x => x.ID == id).FirstOrDefault();
            cafe.Name = name;
            cafe.Location = location;
            cafe.Category = category;
            return RedirectToAction("Index");

        }
    
    }
}